/* 
 * File:   Interpolator.cpp
 * Author: root
 * 
 * Created on October 27, 2015, 1:37 AM
 */

#include "Interpolator.h"
#include <iostream>

using namespace std;

Interpolator* Interpolator::instance = 0;

Interpolator* Interpolator::getInstance()
{
  if ( !Interpolator::instance )
  {
        Interpolator::instance = new Interpolator();
  }
  return Interpolator::instance;
}

Interpolator::Interpolator() {
    cout<<"Interpolator() constructor\n";
}

Interpolator::Interpolator(const Interpolator& orig) {
}

Interpolator::~Interpolator() {
}


RawData* Interpolator::get()
{
    return &this->raw[1];
}

void Interpolator::release()
{
    if ( this->getInstance() )
    {
        delete this->getInstance();
        cout<<"Interpolator::release\n";
    }
}

//int Interpolator::update(const RawData *paraRaw)
//{
//    int result;
//    //(0, 1078116352) = 37.5;(0, 1078935552) = 62.5
//    if ((paraRaw->time > (this->raw[0].time + 37.5)) 
//      && (paraRaw->time < (this->raw[0].time + 62.5)))
//    {
//        this->raw[1].time = paraRaw->time / 2 + this->raw[0].time;
//        this->raw[1].xAxis = (this->raw[1].xAxis + paraRaw->xAxis) / 2;
//        this->raw[1].yAxis = (this->raw[1].yAxis + paraRaw->yAxis) / 2;
//        this->raw[1].zAxis = (this->raw[1].zAxis + paraRaw->zAxis) / 2;
//        result = 1;
//    } else {
//        result = 0;
//    }
//
//    this->raw[0] = *paraRaw;
//    return result;
//}

int Interpolator::update(const RawData *paraRaw)
{
    int result = 0;
    //(0, 1078116352) = 37.5;(0, 1078935552) = 62.5
    if ((paraRaw->time > (this->raw[0].time + 37.5)) 
      && (paraRaw->time < (this->raw[0].time + 62.5)))
    {
        this->raw[1].time = (paraRaw->time - this->raw[0].time) / 2 + this->raw[0].time;
        this->raw[1].xAxis = (this->raw[0].xAxis + paraRaw->xAxis) / 2;
        this->raw[1].yAxis = (this->raw[0].yAxis + paraRaw->yAxis) / 2;
        this->raw[1].zAxis = (this->raw[0].zAxis + paraRaw->zAxis) / 2;
        result = 1;
    } else {
        result = 0;
    }

    this->raw[0] = *paraRaw;
    return result;
}